/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */
package com.gadglet.client.gwt.core.ui;

import com.gadglet.client.gwt.core.GadgletQuery;
import com.gadglet.client.gwt.core.GadgletRequest;
import com.gadglet.client.gwt.core.GadgletResponse;
import com.gadglet.client.gwt.ui.DebugDialogBox;
import com.gadglet.client.gwt.ui.UIUtils;
import com.gadglet.params.ProfileFields;
import com.gadglet.params.ReqActionTypes;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.impl.PreferencesUtil;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Used to present a list of ALL the users from a specific Google Apps account
 * that are using this Gadget service
 *
 */
/**
 * @author user
 *
 */
public class DomainUsersPanel {
DebugDialogBox debug = DebugDialogBox.getErrorNotifier();
	
	protected final PreferencesUtil prefs = PreferencesUtil.nativeInitPrefs();
	
	private final VerticalPanel domainUsersContainer = new VerticalPanel();
	private final VerticalPanel domainUsersList = new VerticalPanel();

		
	final GadgletQuery inviteQry = new GadgletQuery();
	
	final InviteRequest inviteRequest = new InviteRequest(
			inviteQry);
	
	final GadgletQuery viewDomainUsersQry = new GadgletQuery();

	final ViewDomainUsersRequest viewDomainUsersRequest = new ViewDomainUsersRequest(
			viewDomainUsersQry);
		
		
	private static DomainUsersPanel domainUsersPanel = null;
	
	public VerticalPanel getDomainUsersContainer(){
		return this.domainUsersContainer;
	}
		
	
	public static DomainUsersPanel getDomainUsersPanel() {
		if (domainUsersPanel == null) {
			domainUsersPanel = new DomainUsersPanel();
		}
		return domainUsersPanel;
	}
	
	DomainUsersPanel(){
		inviteQry.setRequestAction(ReqActionTypes.INVITE_FRIEND);
		
		domainUsersContainer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		Label domainUsersLabel = new Label();
		domainUsersLabel.setWidth("250px");
		domainUsersLabel.setStylePrimaryName("friendsLabel");
		domainUsersLabel.setText(prefs.getMsg("gadgetLabelDomainColleagues") );
		
		domainUsersContainer.add(domainUsersLabel);
		domainUsersContainer.add(domainUsersList);
			
	}
	
	
	
	/**
	 *  This method activate the request
	 */
	public void viewDomainUsers(){
		domainUsersList.clear();
		viewDomainUsersQry.setRequestAction(ReqActionTypes.GET_DOMAIN_USERS);
		viewDomainUsersRequest.makeRequest();
		
	}
	

	
	
	/**
	 * Representing a request to the Domian users list. The method processResults
	 * is called to process the returning results 
	 *
	 */
	
	public class ViewDomainUsersRequest extends GadgletRequest {
		ViewDomainUsersRequest(GadgletQuery req) {
			super(req);
		}

		@Override
		protected void processResults(GadgletResponse jResponse) {

			if (jResponse.isSuccessful()) {
	
				int inviteNum = jResponse.getRootItemsNum();
				
				

				for (int index = 0; index < inviteNum; index++) {
					JSONObject item = jResponse.getItem(index);
								
					String photo = null;
					String nickName = null;
					String title= null;
					String userId = null;
					
					if(item.get(ProfileFields.PROFILE_THUMBNAILURL.getParamName())!=null)
						photo = item.get(ProfileFields.PROFILE_THUMBNAILURL.getParamName()).isString().stringValue();
					
					if(item.get(ProfileFields.PROFILE_NICKNAME.getParamName())!=null)
						nickName = item.get(ProfileFields.PROFILE_NICKNAME.getParamName()).isString().stringValue();
				
					if(item.get(ProfileFields.PROFILE_TITLE.getParamName())!=null)
						title = item.get(ProfileFields.PROFILE_TITLE.getParamName()).isString().stringValue();
					
					if(item.get(ProfileFields.PROFILE_FRIEND_ID.getParamName())!=null)
						userId = item.get(ProfileFields.PROFILE_FRIEND_ID.getParamName()).isString().stringValue();

					
					try {
						DomainUser domainUser = new DomainUser(nickName,photo,title,userId);

						domainUsersList
								.add(domainUser.getDomainUserItem());
						domainUsersList
						.add(UIUtils.getSpace());
						
					} catch (Exception e) {
						debug.showError(111, e.getMessage());
					}
				}

			} else
				debug.showError(101, jResponse.getError());
			
			
		}
	}
	
	
	
	
	/**
	 * A single domain user class
	 *
	 */
	private class DomainUser {
	
		private String invitedByNickName;
		
		private String friendPhoto;
		
		private String title;
		
		private final String userId;

		public DomainUser(
				String invitedByNickName, String photo, String title, String userId) {
			this.friendPhoto = photo;
			this.title = title;
			this.invitedByNickName = invitedByNickName;
			this.userId = userId;
		}


		HorizontalPanel getDomainUserItem() {
			HorizontalPanel item = new HorizontalPanel();
			VerticalPanel name = new VerticalPanel();
			
			name.add(new HTML(invitedByNickName));
			name.add(new HTML(title));
			
			
			item.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
			item.add(name);
			item.add(UIUtils.getSpace());
			
			if(this.friendPhoto!=null && this.friendPhoto.length()>0){
				Image img = new Image(this.friendPhoto);
				img.setPixelSize(30, 30);
					item.add(img);
				
				item.add(UIUtils.getSpace());
			}
			
			if(userId!=null){
			final Button inviteButton = new Button(prefs.getMsg("gadgetLabelInvite"));
			inviteButton.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					inviteQry.clearParamList();
					inviteQry.addParam(ProfileFields.PROFILE_FRIEND_ID.getParamName(), userId);
					inviteRequest.makeRequest();
				
					inviteButton.setEnabled(false);
				}
			});
			
			item.add(inviteButton);
			}
			
			
			return item;
		}

	}
	
	
	/**
	 * A request class for user invitation
	 *
	 */
	public class InviteRequest extends GadgletRequest {
		InviteRequest(GadgletQuery req) {
			super(req);
		}

		@Override
		protected void processResults(GadgletResponse data) {
			GadgletResponse jResponse = data;

			if (jResponse.isSuccessful()) {
				DomainFriendsPanel.getDomainFriendsPanel().viewDomainFriends();
				

			} else {

				debug.showError(101, jResponse.getError());
			}

		}
	}

}
